package service

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
)

//@author: [piexlmax](https://github.com/piexlmax)
//@function: CreateIdCard
//@description: 创建IdCard记录
//@param: idCard model.IdCard
//@return: err error

func CreateIdCard(idCard model.IdCard) (err error) {
	err = global.GVA_DB.Create(&idCard).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteIdCard
//@description: 删除IdCard记录
//@param: idCard model.IdCard
//@return: err error

func DeleteIdCard(idCard model.IdCard) (err error) {
	err = global.GVA_DB.Delete(idCard).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteIdCardByIds
//@description: 批量删除IdCard记录
//@param: ids request.IdsReq
//@return: err error

func DeleteIdCardByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]model.IdCard{},"id in ?",ids.Ids).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: UpdateIdCard
//@description: 更新IdCard记录
//@param: idCard *model.IdCard
//@return: err error

func UpdateIdCard(idCard model.IdCard) (err error) {
	err = global.GVA_DB.Save(&idCard).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetIdCard
//@description: 根据id获取IdCard记录
//@param: id uint
//@return: err error, idCard model.IdCard

func GetIdCard(id uint) (err error, idCard model.IdCard) {
	err = global.GVA_DB.Where("id = ?", id).First(&idCard).Error
	return
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetIdCardInfoList
//@description: 分页获取IdCard记录
//@param: info request.IdCardSearch
//@return: err error, list interface{}, total int64

func GetIdCardInfoList(info request.IdCardSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&model.IdCard{})
    var idCards []model.IdCard
    // 如果有条件搜索 下方会自动创建搜索语句
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Find(&idCards).Error
	return err, idCards, total
}